package com.travel.controller;

import com.travel.config.oss.OSSAliyunFileStorageService;
import com.travel.result.Result;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * @author: libin
 * Date: 2025/6/11 14:49
 * Description:
 **/
@ApiOperation("上传文件接口")
@RestController
@RequestMapping("/travel/products")
public class UploadFileController {


    @Autowired
    private OSSAliyunFileStorageService fileStorageService;

    @SneakyThrows
    @ApiOperation("上传文件")
    @PostMapping("/upload")
    public Result<String> uploadFile(MultipartFile file) {
        //文件名--->UUID.后缀
        //abc.jpg
        String originalFilename = file.getOriginalFilename();
        String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
        String filename = UUID.randomUUID().toString() + extension;
        //把文件上传到oss中
        String url = fileStorageService.store(filename, file.getInputStream());

        return Result.success(url);
    }
}
